package com.xjrsoft.module.app.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.toolkit.MPJWrappers;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.enums.AuthorizeType;
import com.xjrsoft.common.enums.EnabledMark;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.app.entity.AppAuthorize;
import com.xjrsoft.module.app.entity.AppMenu;
import com.xjrsoft.module.app.mapper.AppAuthorizeMapper;
import com.xjrsoft.module.app.mapper.AppMenuMapper;
import com.xjrsoft.module.app.service.IAppMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjrsoft.module.organization.entity.UserRoleRelation;
import com.xjrsoft.module.organization.mapper.UserRoleRelationMapper;
import com.xjrsoft.module.system.dto.MenuTreeDto;
import com.xjrsoft.module.system.entity.Authorize;
import com.xjrsoft.module.system.entity.Menu;
import com.xjrsoft.module.system.entity.Subsystem;
import com.xjrsoft.module.system.vo.MenuTreeVo;
import com.xjrsoft.module.system.vo.MenuVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tzx
 * @since 2023-07-26
 */
@Service
@AllArgsConstructor
public class AppMenuServiceImpl extends ServiceImpl<AppMenuMapper, AppMenu> implements IAppMenuService {

    private final UserRoleRelationMapper userRoleRelationMapper;

    private final AppAuthorizeMapper appAuthorizeMapper;

    @Override
    public List<AppMenu> getAppAuthMenuList(MenuTreeDto dto) {
        List<UserRoleRelation> relations = userRoleRelationMapper.selectList(Wrappers.lambdaQuery(UserRoleRelation.class)
                .select(UserRoleRelation::getRoleId)
                .eq(UserRoleRelation::getUserId, StpUtil.getLoginIdAsLong()));
        if (CollectionUtils.isEmpty(relations)) {
            return new ArrayList<>();
        }
        List<Long> roleIds = relations.stream().map(UserRoleRelation::getRoleId).collect(Collectors.toList());
        List<Long> authMenuIdList = null;
        // 非超级管理员权限过滤
        if (!roleIds.contains(GlobalConstant.SUPER_ADMIN_ROLE_ID)) {
            List<AppAuthorize> authorizeList = appAuthorizeMapper.selectList(Wrappers.<AppAuthorize>lambdaQuery()
                    .eq(AppAuthorize::getAppAuthorizeType, AuthorizeType.MENU.getCode())
                    .in(AppAuthorize::getRoleId, roleIds));
            if (CollectionUtils.isEmpty(authorizeList)) {
                return new ArrayList<>();
            }
            authMenuIdList = authorizeList.stream().map(AppAuthorize::getObjectId).collect(Collectors.toList());
        }

        return this.list(Wrappers.<AppMenu>lambdaQuery()
                .like(StrUtil.isNotBlank(dto.getCode()), AppMenu::getCode, dto.getCode())
                .like(StrUtil.isNotBlank(dto.getName()), AppMenu::getName, dto.getName())
                .in(CollectionUtils.isNotEmpty(authMenuIdList), AppMenu::getId, authMenuIdList)
                .eq(AppMenu::getEnabledMark, EnabledMark.ENABLED.getCode())
                .orderByAsc(AppMenu::getSortCode)
        );
    }
}
